﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace WinformCommonDemo.CommControlOperate
{
    public static class JsonHelper
    {
        static JavaScriptSerializer js = new JavaScriptSerializer();
        public static string ToJson(this object obj)
        {
            try
            {
                return js.Serialize(obj);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public static T ToObj<T>(this string json)
        {
            try
            {
                return js.Deserialize<T>(json);
            }
            catch (Exception e)
            {
                return default(T);
            }
        }
    }
}
